//
//  JExpandHeader.m
//  iBuy
//
//  Created by junFung on 15/10/3.
//  Copyright (c) 2015年 yc. All rights reserved.
//

#define JExpandContentOffset @"contentOffset"

#import "JExpandHeader.h"

@implementation JExpandHeader{
    __weak UIScrollView *_scrollView; //scrollView或者其子类
    __weak UIView *_expandView; //背景可以伸展的View
    
    CGFloat _expandHeight;
}

- (void)dealloc{
    if (_scrollView) {
        [_scrollView removeObserver:self forKeyPath:JExpandContentOffset];
        _scrollView = nil;
    }
    _expandView = nil;
}

+ (id)expandWithScrollView:(UIScrollView*)scrollView expandView:(UIView*)expandView{
    JExpandHeader *expandHeader = [JExpandHeader new];
    [expandHeader expandWithScrollView:scrollView expandView:expandView];
    return expandHeader;
}

- (void)expandWithScrollView:(UIScrollView*)scrollView expandView:(UIView*)expandView{
    
    _expandHeight = CGRectGetHeight(expandView.frame);
    _scrollView = scrollView;
    _scrollView.contentInset = UIEdgeInsetsMake(_expandHeight, 0, 0, 0);
    [ _scrollView insertSubview:expandView atIndex:0];
    [ _scrollView addObserver:self forKeyPath:JExpandContentOffset options:NSKeyValueObservingOptionNew context:nil];
    [ _scrollView setContentOffset:CGPointMake(0, -180)];
    
    _expandView = expandView;
    // [ _scrollView setCanCancelContentTouches:NO] ;
    [_scrollView setCanCancelContentTouches:YES];
    [ expandView setUserInteractionEnabled:YES];
    
    /*********************************************************************************/
    //UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)] ;
    //[ _scrollView addGestureRecognizer:singleTap];
    /*********************************************************************************/
    
    //使View可以伸展效果  重要属性
    _expandView.contentMode= UIViewContentModeScaleAspectFill;
    _expandView.clipsToBounds = YES;
    
    [ self reSizeView ] ;
}

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
    //CGPoint touchPoint = [ gesture locationInView : _scrollView ] ;
    NSLog( @"-------singleTapGestureCaptured----------" ) ;
}


// How to pass touch event frome a UIScrollView's subview to it's superview
// UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self         action:@selector(singleTapGestureCaptured:)];
// [scrollView addGestureRecognizer:singleTap];


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    NSLog(@"- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch" ) ;
    return YES;
}

- (void)singleTapGestureCaptured0:(UITapGestureRecognizer *)gesture
{
    //CGPoint touchPoint=[gesture locationInView : _scrollView];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if (![keyPath isEqualToString:JExpandContentOffset]) {
        return;
    }
    [self scrollViewDidScroll:_scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    
    CGFloat offsetY = scrollView.contentOffset.y;
    if(offsetY < _expandHeight * -1) {
        CGRect currentFrame = _expandView.frame;
        currentFrame.origin.y = offsetY;
        currentFrame.size.height = -1*offsetY;
        _expandView.frame = currentFrame;
    }
    
}

- (void)reSizeView{
    
    //重置_expandView位置
    [_expandView setFrame:CGRectMake(0, -1*_expandHeight, CGRectGetWidth(_expandView.frame), _expandHeight)];
    
}

@end
